package com.manage.customer.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.manage.customer.dao.CustomerMapper;
import com.manage.customer.dao.CustomerRepository;
import com.manage.customer.entity.Customer;
import com.manage.customer.service.CustomerService;
import com.manage.utils.BeanUtil;
import com.manage.utils.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Fitch
 */
@Service
public class CustomerServiceImpl implements CustomerService{

    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 新增、修改
     * @param customer
     * @return
     */
    @Override
    public Customer saveOrUpdateCustomer(Customer customer) {
        if(customer.getId()==null){
          //新增
            Date current = new Date();
            customer.setCreateTime(current);
            customer=this.customerRepository.save(customer);
        }else {
          //修改
            Customer local = this.customerRepository.findOne(customer.getId());
            if(local==null){
               return null;
            }
            BeanUtil.copyPropertiesIgnoreNull(customer,local);
            customer = this.customerRepository.save(local);
        }
        return customer;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public Customer getById(Long id) {
        return this.customerRepository.findOne(id);
    }

    /**
     * 分页
     * @param criteria
     * @return
     */
    @Override
    public List<Customer> findList(Criteria criteria) {
        return null;
    }
}
